package com.whu.lsj;

import org.junit.Test;

import java.io.*;

/**
 * 评估分词效果
 * Created by Enginner-Jie on 2016/8/25.
 *  正确率 = 正确识别的个体总数 /  识别出的个体总数
 *  召回率 = 正确识别的个体总数 /  测试集中存在的个体总数
 */
public class AssessCRF {
		/**
		 * V1.0
		 * 测试结果比对测试语料---> 正确识别的个体总数
		 * 测试结果标注个数---> 识别出的个体总数
		 */
//		protected static void evalute(){
//			int all_num = 0;   //测试集中术语总数
//			int recognized_num = 0;  //识别出的术语总数
//			int correct_num = 0;   //识别出的正确术语总数
//
//			File crf_train = new File(WordSeg.crf_train_file);  //CRF训练语料
//			File crf_result = new File(WordSeg.crf_result_file);  //测试结果
//			BufferedReader br = null;
//			BufferedReader br2 = null;
//			try{
//				br = new BufferedReader(new FileReader(crf_train));
//				br2 = new BufferedReader(new FileReader(crf_result));
//				String train_line;
//				String result_line;
//				while(((train_line = br.readLine()) != null)&&((result_line = br2.readLine()) != null)){
//						if (train_line.equals("")||result_line.equals("")){
//							continue;
//						}
////						if (result_line.equals("")){
////							continue;
////						}
//						char train_label = train_line.charAt(train_line.length()-1);  //测试结果标注
//						char result_label = result_line.charAt(result_line.length()-1);  //测试语料标注
//
//						//排除空行和非术语词
//						if (train_label != 'N'){
//							all_num ++;
//							if (result_label != 'N'){
//								recognized_num ++;
//								if (train_label == result_label)
//									correct_num ++;
//							}
//						}
//					}
//				System.out.println("测试语料中的术语总数："+ all_num);
//				System.out.println("CRF识别出的术语总数："+ recognized_num);
//				System.out.println("识别出的正确术语总数："+ correct_num);
//
//				double P = correct_num*1.0 / recognized_num;  //Precision准确率
//				double R = recognized_num*1.0/ all_num;           //Recall召回率
//				double F = P*R/(P + R);       //F 值
//
//				System.out.println("准确率："+ P);
//				System.out.println("召回率："+ R);
//				System.out.println("F值   ："+ F);
//
//			}catch (IOException e){
//				e.printStackTrace();
//			}finally{
//				if (br != null){
//					try {
//						br.close();
//					} catch (IOException e) {
//						e.printStackTrace();
//					}
//				}
//				if (br2 != null){
//					try {
//						br2.close();
//					} catch (IOException e) {
//						e.printStackTrace();
//					}
//				}
//			}
//	}

	/**
	 * v2.0 整体术语评价
	 */
	public static void evalute(){
		int all_num = 0;   //测试集中术语总数
		int recognized_num = 0;  //识别出的术语总数
		int correct_num = 0;   //识别出的正确术语总数

		int line = 0;

		File crf_train = new File(WordSeg.crf_train_file);  //CRF训练语料
		File crf_result = new File(WordSeg.crf_result_file);  //测试结果
		BufferedReader br = null;
		BufferedReader br2 = null;
		try {
			br = new BufferedReader(new FileReader(crf_train));
			br2 = new BufferedReader(new FileReader(crf_result));
			String train_line;
			String result_line;
			StringBuffer _terms_train = new StringBuffer();  //训练预料中的术语词，包括多个和单个词
			StringBuffer _terms_result = new StringBuffer();  //分词结果中术语词，包括多个和单个词
			while (((train_line = br.readLine()) != null) && ((result_line = br2.readLine()) != null)) {
				line++;
				if (train_line.equals("") || result_line.equals("")) {
					continue;
				}

				String train_ss[] = train_line.split("\t");
				String train_term = train_ss[0];
				String train_label = train_ss[1];

				String result_ss[] = result_line.split("\t");
				String result_term = result_ss[0];
				String result_label = result_ss[2];


				if (train_label.equals("S")||train_label.equals("E")) {
					_terms_train.append(train_term);
					all_num ++;
				}else if (train_label.equals("B")||train_label.equals("M")){
					_terms_train.append(train_term + " ");
				}else {
				//	_terms_result = new StringBuffer();
				}

				if (result_label.equals("S")||result_label.equals("E")) {
					_terms_result.append(result_term);
					recognized_num ++;
				}else if (result_label.equals("B")||result_label.equals("M")){
					_terms_result.append(result_term + " ");
				}else {
				//	_terms_result = new StringBuffer();
				}
				/*使用toString() 方法将StringBuffer转换为String,再使用equals()方法比较。*/
				//识别正确的
				if ((train_label.equals("S")||train_label.equals("E"))&&(result_label.equals("S")||result_label.equals("E"))){
					if (_terms_train.toString().equals(_terms_result.toString())) {
						correct_num++;
					}
					_terms_train = new StringBuffer();
					_terms_result = new StringBuffer();
				}
			}
				System.out.println("测试语料中的术语总数："+ all_num);
				System.out.println("CRF识别出的术语总数："+ recognized_num);
				System.out.println("识别出的正确术语总数："+ correct_num);

				float P = correct_num * 1.0f / recognized_num;  //Precision准确率
			    float R = recognized_num * 1.0f/ all_num;       //Recall召回率
			    float F = P*R*2.0f/(P + R);       //F 值

				System.out.printf("准确率：%.2f%n",P*100);
				System.out.printf("召回率：%.2f%n",R*100);
				System.out.printf("F值:   %.2f%n",F*100);

		}catch(IOException e){
			e.printStackTrace();
		}finally {
			try {
				br.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			try {
				br2.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	public static  void  main(String args[]){
		evalute();
	}
}
